package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.alink.business.alink.ALinkBusinessEx;
import com.aliyun.alink.sdk.net.anet.api.AError;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryRequest;
import com.aliyun.alink.sdk.net.anet.api.transitorynet.TransitoryResponse;
import com.argrace.smart.R;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.entity.Key_ValueEntity;
import com.argrace.smart.entity.MineZigBeeDeviceListEntity;
import com.argrace.smart.glide.GliderImageLoaderUtil;
import com.argrace.smart.util.AlinkHttpUtil;
import com.zhouyou.view.seekbar.SignSeekBar;

import java.util.HashMap;

/**
 * Created by argrace_dev_1 on 2018/2/12.
 */

public class DeviceControl_PM25Activity extends BaseActivity {
    private SignSeekBar seekBar_shidu, seekBar_wendu, seekBar_zhishu, seekBar_tvoc;
    private TextView shidu_text, wendu_text, zhishudu_text, tvoc_text;
    MineZigBeeDeviceListEntity bean;
    private ImageView img;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_devicecontrol_pm25);
        init();
    }

    private void init() {
        bean = (MineZigBeeDeviceListEntity) getIntent().getSerializableExtra("deviceinfo");
        setView();
        showRight(true, R.mipmap.icon_modify);
        //进入设备修改界面
        setLeftandRightClickListeners(null, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(DeviceControl_PM25Activity.this, DeviceModifyActivity.class);
                intent.putExtra("uuid", bean.getUuid());
                intent.putExtra("model", bean.getModel());
                startActivity(intent);
            }
        });
        getDeviceDetailInfo();
    }

    private void setView() {
        img = (ImageView) findViewById(R.id.activity_devicecontrol_img);
        String imgurl = null;
        setCanClick(false);
        seekBar_shidu = (SignSeekBar) findViewById(R.id.activity_devicecontrol_pm_shidu);
        seekBar_wendu = (SignSeekBar) findViewById(R.id.activity_devicecontrol_pm_wendu);
        seekBar_zhishu = (SignSeekBar) findViewById(R.id.activity_devicecontrol_pm_zhishu);
        seekBar_tvoc = (SignSeekBar) findViewById(R.id.activity_devicecontrol_pm_tvoc);
        shidu_text = (TextView) findViewById(R.id.activity_devicecontrol_pm_shidu_text);
        wendu_text = (TextView) findViewById(R.id.activity_devicecontrol_pm_wendu_text);
        zhishudu_text = (TextView) findViewById(R.id.activity_devicecontrol_pm_zhishu_text);
        tvoc_text = (TextView) findViewById(R.id.activity_devicecontrol_pm_tvoc_text);
        if (bean.getModel().equals(ConstantConfigure.MODEL_PM25CHUANGANQI)) {
            shidu_text.setText("当前湿度");
            wendu_text.setText("当前温度");
            zhishudu_text.setText("PM2.5指数");
            tvoc_text.setText("TVOC/VOC浓度");
            imgurl = ConstantConfigure.IMGURL_PM25CHUANGANQI_00011e0f;
            seekBar_shidu.getConfigBuilder()
//                .min(0)
//                .max(4)
//                .progress(2)
//                .sectionCount(4)
//                .thumbColor(ContextCompat.getColor(getContext(), R.color.color_60))
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("%")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
            seekBar_wendu.getConfigBuilder()
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("℃")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
            seekBar_zhishu.getConfigBuilder()
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("μg/m³")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
            seekBar_tvoc.getConfigBuilder()
                    .min(0)
                    .max(10)
                    .progress(2)
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("mg/m³")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_KONGQIHEZI)) {
            shidu_text.setText("当前湿度");
            wendu_text.setText("当前温度");
            zhishudu_text.setText("PM2.5指数");
            tvoc_text.setText("当前电量");
            imgurl = ConstantConfigure.IMGURL_KONGQIHEZI_00011e7e;
            seekBar_shidu.getConfigBuilder()
//                .min(0)
//                .max(4)
//                .progress(2)
//                .sectionCount(4)
//                .thumbColor(ContextCompat.getColor(getContext(), R.color.color_60))
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("%")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
            seekBar_wendu.getConfigBuilder()
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("℃")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
            seekBar_zhishu.getConfigBuilder()
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("μg/m³")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
            seekBar_tvoc.getConfigBuilder()
                    .min(0)
                    .max(100)
                    .progress(2)
                    .sectionTextColor(ContextCompat.getColor(DeviceControl_PM25Activity.this, R.color.colorPrimary))
                    .sectionTextSize(15)
                    .setUnit("%")//setUnit("μmol/l");setUnit("μ/l") setUnit("m")
                    .build();
        }
        if (imgurl != null) {
            GliderImageLoaderUtil.load(this, imgurl, img);
        }

    }

    //查询设备最新状态
    private void getDeviceDetailInfo() {
        String method = "mtop.openalink.app.core.device.get.status";
        HashMap<String, Object> params = new HashMap<>();
        params.put("uuid", bean.getUuid());
        AlinkHttpUtil.AlinkHttpRequest(method, params, new ALinkBusinessEx.IListener() {
            @Override
            public void onSuccess(TransitoryRequest transitoryRequest, TransitoryResponse transitoryResponse) {
                if (transitoryResponse == null || transitoryResponse.data == null) {
                    return;
                }
                Log.i("info", "子设备最新状态成功" + transitoryResponse.data);
                JSONObject obj = JSON.parseObject(transitoryResponse.data.toString());
                fillData(obj, obj.getString("uuid"));
            }

            @Override
            public void onFailed(TransitoryRequest transitoryRequest, AError aError) {
                Log.i("info", "子设备最新状态失败" + aError.getMsg() + aError.getCode());
            }
        });
    }

    private void fillData(JSONObject obj, String uuid) {
        if (uuid == null || !uuid.equals(bean.getUuid())) {
            return;
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_PM25CHUANGANQI)) {
            Key_ValueEntity currentHumidity = JSON.parseObject(obj.getString("CurrentHumidity"), Key_ValueEntity.class);
            Key_ValueEntity currentTemperature = JSON.parseObject(obj.getString("CurrentTemperature"), Key_ValueEntity.class);
            Key_ValueEntity pM25 = JSON.parseObject(obj.getString("PM25"), Key_ValueEntity.class);
            Key_ValueEntity tVOC = JSON.parseObject(obj.getString("TVOC"), Key_ValueEntity.class);
            if (currentHumidity != null) {
                float progress_shidu = (float) Double.parseDouble(currentHumidity.getValue());
                seekBar_shidu.setProgress(progress_shidu);
            }
            if (currentTemperature != null) {
                float progress_wendu = (float) Double.parseDouble(currentTemperature.getValue());
                seekBar_wendu.setProgress(progress_wendu);
            }
            if (pM25 != null) {
                float progress_zhishu = (float) Double.parseDouble(pM25.getValue());
                seekBar_zhishu.setProgress(progress_zhishu);
            }
            if (tVOC != null) {
                float progress_tvoc = (float) Double.parseDouble(tVOC.getValue());
                seekBar_tvoc.setProgress(progress_tvoc);
            }
        }
        if (bean.getModel().equals(ConstantConfigure.MODEL_KONGQIHEZI)) {
            Key_ValueEntity currentHumidity = JSON.parseObject(obj.getString("CurrentHumidity"), Key_ValueEntity.class);
            Key_ValueEntity currentTemperature = JSON.parseObject(obj.getString("CurrentTemperature"), Key_ValueEntity.class);
            Key_ValueEntity pM25 = JSON.parseObject(obj.getString("PM25"), Key_ValueEntity.class);
            Key_ValueEntity tVOC = JSON.parseObject(obj.getString("BatteryPercentage"), Key_ValueEntity.class);
            if (currentHumidity != null) {

                float progress_shidu = (float) Double.parseDouble(currentHumidity.getValue());
                seekBar_shidu.setProgress(progress_shidu);
            }
            if (currentTemperature != null) {

                float progress_wendu = (float) Double.parseDouble(currentTemperature.getValue());
                seekBar_wendu.setProgress(progress_wendu);
            }
            if (pM25 != null) {

                float progress_zhishu = (float) Double.parseDouble(pM25.getValue());
                seekBar_zhishu.setProgress(progress_zhishu);
            }
            if (tVOC != null) {
                float progress_tvoc = (float) Double.parseDouble(tVOC.getValue());
                seekBar_tvoc.setProgress(progress_tvoc);
            }


        }


    }

    @Override
    protected void dorecieve(JSONObject obj) {
        super.dorecieve(obj);
        if (obj == null || obj.getJSONObject("params") == null || obj.getJSONObject("params").getJSONObject("data") == null || obj.getJSONObject("params").getString("uuid") == null) {
            return;
        }
        fillData(obj.getJSONObject("params").getJSONObject("data"), obj.getJSONObject("params").getString("uuid"));
    }

    private void setCanClick(boolean value) {
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        if (value) {
        } else {
            ll.setOnClickListener(null);
            ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    toastActBtm("不可点击");
                }
            });
        }
    }
}
